package com.globalwave.common.appframe.module
{
	import com.globalwave.common.EditWindow;
	import com.globalwave.common.appframe.GlobalContext;
	import com.globalwave.common.appframe.MainFormInf;
	import com.globalwave.common.appframe.ModuleInf;
	import com.globalwave.common.event.RecordEvent;
	import com.globalwave.util.HttpServiceUtil;
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.modules.Module;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	
	public class AppModule extends Module implements ModuleInf
	{
		/**
		 *  主要支持选择数据后，关闭窗口时，发出事件
		 *
		 *  @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_MINIMIZE
		 */
		[Event(name=RecordEvent.POPUP_SELECTED, type="com.globalwave.common.event.RecordEvent")]
		
		
		public var buttons:ArrayCollection ;
		
		public var windowClass:Class ;
		public var windowObject:EditWindow ;
		
		public var deleteUrl:String;
		
		public var globalApp:DisplayObject;
		
		public var parentModule:UIComponent ;
		
		public var idName:String ;
		
		public var pidOffSet:int = -1;
		
		
		[Embed(source="/com/globalwave/image/button/Add.png")]
		public var addIcon:Class;
		
		[Embed(source="/com/globalwave/image/button/Edit.png")]
		public var editIcon:Class;
		
		[Embed(source="/com/globalwave/image/button/Delete.png")]
		public var deleteIcon:Class;
		
		[Embed(source="/com/globalwave/image/button/Refresh.png")]
		public var refreshIcon:Class;
		
		
		
		public function AppModule()
		{
			super();
			
			this.percentHeight = 100 ;
			this.percentWidth = 100 ;
			this.layout="absolute" ;
			
			this.globalApp = DisplayObject(FlexGlobals.topLevelApplication) ;
		}
		
		public function getGlobalApp():MainFormInf {
			return FlexGlobals.topLevelApplication as MainFormInf ;
		}
		
		public function getGlobalContext():GlobalContext {
			return this.getGlobalApp().getGlobalContext() ;
		}
		
		public function getButtons():ArrayCollection{
			
			if (buttons == null) {
				buttons = new ArrayCollection() ;
				buttons.addItem({
					icon:refreshIcon,
					label:"Refresh",
					click:"refresh",
					pid:pidOffSet == -1?-1:pidOffSet + 1
				});
				buttons.addItem({
					icon:addIcon,
					label:"Add",
					click:"add",
					pid:pidOffSet == -1?-1:pidOffSet + 0
				});
				buttons.addItem({
					icon:editIcon,
					label:"Edit",
					click:"edit",
					pid:pidOffSet == -1?-1:pidOffSet + 2
				});
				buttons.addItem({
					icon:deleteIcon,
					label:"Delete",
					click:"delete",
					pid:pidOffSet == -1?-1:pidOffSet + 3
				});
			}
			return buttons ;
		}
		
		
		public function refreshOnDataUpdate(event:RecordEvent):void {
		
		}
		
		public function getSelectedItems():Array {
		    return null ;
		}
		
		public function onButtonClick(button:Object):Object {
			
			var editWindow:EditWindow = null ;
			
			if (button.click == "add") {
				if (windowObject == null) {
				    editWindow = EditWindow(PopUpManager.createPopUp(this.globalApp, windowClass, true));
				} else {
					editWindow = windowObject;
					editWindow.updateObject = null ;
					PopUpManager.addPopUp(windowObject, this.globalApp, true);
					editWindow.init() ;
				}
				
				editWindow.addEventListener(RecordEvent.ADD_SUCCESS, refreshOnDataUpdate);
				
			} else if (button.click == "edit") {
				if (getSelectedItems() == null || getSelectedItems().length != 1) {
					Alert.show("请选择要编辑的记录！") ;
					return null;
				}
				
				if (windowObject == null) {
				    editWindow = EditWindow(PopUpManager.createPopUp(this.globalApp, windowClass, true));
				} else {
					editWindow = windowObject;
					PopUpManager.addPopUp(windowObject, this.globalApp, true);
				}
				
				editWindow.updateObject = this.getSelectedItems()[0] ;
				
				if (windowObject != null) {
					editWindow.init() ;
				}
				
				editWindow.addEventListener(RecordEvent.UPDATE_SUCCESS,refreshOnDataUpdate);
				
			} else if (button.click == "delete") {
				
				var deleteObjects:Array = this.getSelectedItems() ;
				
				if (deleteObjects == null) {
					Alert.show( "请选择记录！" );
					return null;
				}
				
				var msg : String ;
				if (deleteObjects == null || deleteObjects.length == 1) {
					msg = "是否删除[" + deleteObjects[0][idName] + "]？" ;
				} else {
					msg = "是否删除所有选中记录？" ;
				}
				Alert.show( msg , "删除确认", Alert.YES|Alert.NO, null, deleteRecordConfirm);      
			} 
			
			return editWindow;
		}
		
		
		private function deleteRecordConfirm(event:CloseEvent):void {
			
			
			if (event.detail==Alert.YES) {
				var params :Object = new Object();
				
				params["012345678"] = "012345678" ;
				var ids : ArrayCollection = new ArrayCollection() ;
				var deleteObjects:Array = this.getSelectedItems() ;
				for (var i : int = 0 ; i < deleteObjects.length ; i ++) {
					ids.addItem(deleteObjects[i][idName]) ;
				}
				
				params["ids"] = ids.toArray() ;
				
				var httpService:HTTPService = HttpServiceUtil.getService(this.deleteUrl);
				
				httpService.addEventListener(ResultEvent.RESULT, deleteRecordOnReturn) ;
				
				httpService.send(params);     
				
			}            
		}
		
		protected function deleteRecordOnReturn(event:ResultEvent) : void {
			//displayObject.dispatchEvent(new RecordEvent(RecordEvent.DELETE_SUCCESS)) ;
			this.refreshOnDataUpdate(null);
		}  
	}
}